﻿using CQRSLearn.Domain;
using CQRSLearn.Persistence;
using MediatR;

namespace CQRSLearn.Features.Products.Commands.Create
{
    public class CreateProductCommandHandler : IRequestHandler<CreateProductCommand, Guid>
    {
        private readonly AppDbContext context;

        public CreateProductCommandHandler(AppDbContext context)
        {
            this.context = context;
        }

        public async Task<Guid> Handle(CreateProductCommand request, CancellationToken cancellationToken)
        {
            var product = new Product(request.Name,request.Description,request.Price);
            await context.Products.AddAsync(product);
            await context.SaveChangesAsync();
            return product.Id;
        }
    }
}
